"use strict";


const Joi = require('joi');
const Result = require("../basics/result");
const CheckUtil = require("../utils/check");
const BasicsCtl = require("../basics/clz/basectl");
const HomeImpl = require("../service/home");
const MappApi = require("../taobao/mapp");

class HomeCtl extends BasicsCtl {

  constructor(context) {
    super(context);
    this.homeImpl = new HomeImpl(this.cloud);
  }

  async bindApp() {
    const rule = {
      app_id: Joi.string().required(),
      version: Joi.string().required(),
      shopseesion: Joi.string()
    }
    CheckUtil.validate(rule, this.params);
    let { app_id, version, shopseesion, accessToken } = this.params;
    if (!shopseesion) {
      shopseesion = accessToken;
    }
    return await MappApi.minicomplete(this.cloud, shopseesion, app_id, version)
  }

  /**
   * 查询列表
   */
  async findMasterList() {
    const rule = {
      pageIndex: Joi.number().integer().required(),
      pageSize: Joi.number().integer().required(),
      isband: Joi.number().integer().required()
    }
    CheckUtil.validate(rule, this.params);
    return await this.homeImpl.queryMasterList(this.params);
  }

  /**
   * 初始化用户
   */
  async initMaster() {
    return await this.homeImpl.initMaster(this.params);
  }

  /**
   * 修改用户
   */
  async editMaster() {
    const rule = {
      id: Joi.number().integer().required(),
      shopId: Joi.string().required(),
      sellerId: Joi.string().required()
    }
    CheckUtil.validate(rule, this.params);
    return await this.homeImpl.editMaster(this.params);
  }

  /**
   * 修改用户
   */
  async updateAppId() {
    const rule = {
      userId: Joi.number().integer().required(),
      appId: Joi.string().required()
    }
    CheckUtil.validate(rule, this.params);
    return await this.homeImpl.editAppId(this.params);
  }

  /**
   * 查询labels
   */
  async findMasterLabels() {
    return await this.homeImpl.quereyMasterLabels(this.params);
  }

  /**
   * 查询labels
   */
  async findAppLabels() {
    return await this.homeImpl.quereyAppLabels(this.params);
  }

  /**
   * 查询labels
   */
  async findLiveCard() {
    const rule = {
      id: Joi.number().integer().required()
    }
    CheckUtil.validate(rule, this.params);
    return await this.homeImpl.queryLiveCardData(this.params);
  }

  /**
   * 查询labels
   */
  async editLiveCard() {
    const rule = {
      id: Joi.number().integer().required(),
      ctxt: Joi.string().required()
    }
    CheckUtil.validate(rule, this.params);
    return await this.homeImpl.editLiveCardData(this.params);
  }

  /**
   * 执行sql
   */
  async executeSql() {
    const rule = {
      sql: Joi.string().required()
    }
    CheckUtil.validate(rule, this.params);
    return await this.homeImpl.executeSql(this.params);
  }

}
module.exports = HomeCtl;